//
//  SFPMoreRecordsController.swift
//  StriveForPregnancy
//
//  Created by iOS-Czz on 2023/11/9.
//

import UIKit
import BRPickerView
import ProgressHUD

class SFPMoreRecordsController: SFPBaseViewController {

    @IBOutlet var dateTF: UITextField!
    @IBOutlet var daysTF: UITextField!
    @IBOutlet var weightTF: UITextField!
    @IBOutlet var tyreTF: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    @IBAction func gotoBack(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    @IBAction func gotoSave(_ sender: Any) {
        self.view.endEditing(true)
        if self.dateTF.text!.isEmpty {
            showTips("Please select record date")
            return
        }
        if self.daysTF.text!.isEmpty {
            showTips("Please select preguancy days")
            return
        }
        if self.weightTF.text!.isEmpty{
            showTips("Please select weight")
            return
        }
        if self.tyreTF.text!.isEmpty {
            showTips("Please select tire circumference")
            return
        }
        let model = SFPBodyRecordModel()
        model.date = self.dateTF.text!
        model.days = self.daysTF.text!
        model.weight = self.weightTF.text!
        model.tyre = self.tyreTF.text!         
        model.isDefault = false
        model.saveToDB()
        let helper = SFPBodyRecordModel.getUsingLKDBHelper()
        helper.delete(with: SFPBodyRecordModel.self, where: "isDefault = true")
        ProgressHUD.showSuccess("More record addition succeed")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popViewController(animated: true)
        }
    }
    @IBAction func gotoSelectRecordDate(_ sender: Any) {
        self.view.endEditing(true)
        let pickerView = BRDatePickerView(pickerMode: .YMD)
        let style = BRPickerStyle(themeColor: THEM_COLOR)
        style.language = "en"
        pickerView.pickerStyle = style
        pickerView.resultBlock = { selectDate,selectValue in
            self.dateTF.text = selectDate?.toString(format: "yyyy.MM.dd")
        }
        pickerView.show()
    }
    @IBAction func gotoSelectDays(_ sender: Any) {
        self.view.endEditing(true)
        let pickerView = BRStringPickerView(pickerMode: .componentSingle)
        let style = BRPickerStyle(themeColor: THEM_COLOR)
        style.language = "en"
        pickerView.pickerStyle = style
        pickerView.dataSourceArr = self.getDays()
        pickerView.resultModelBlock = { results in
            self.daysTF.text = String(format: "%@", results!.value!)
        }
        pickerView.show()
    }
    @IBAction func gotoSelectWeight(_ sender: Any) {
        self.view.endEditing(true)
        let pickerView = BRStringPickerView(pickerMode: .componentMulti)
        let style = BRPickerStyle(themeColor: THEM_COLOR)
        style.language = "en"
        pickerView.pickerStyle = style
        pickerView.dataSourceArr = self.getWeights()
        pickerView.resultModelArrayBlock = { results in
            self.weightTF.text = String(format: "%@.%@", (results?.first?.value)!,(results?.last?.value)!)
        }
        pickerView.show()
    }
    @IBAction func gotoSelectTyre(_ sender: Any) {
        self.view.endEditing(true)
        let pickerView = BRStringPickerView(pickerMode: .componentMulti)
        let style = BRPickerStyle(themeColor: THEM_COLOR)
        style.language = "en"
        pickerView.pickerStyle = style
        pickerView.dataSourceArr = self.getTyres()
        pickerView.resultModelArrayBlock = { results in
            self.tyreTF.text = String(format: "%@.%@", (results?.first?.value)!,(results?.last?.value)!)
        }
        pickerView.show()
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
extension SFPMoreRecordsController{
    func getDays() -> [String] {
        var days: [String] = []
        for temp in 1...365 {
            days.append(String(temp))
        }
        return days
    }
    func getWeights() -> [[String]] {
        var weightData: [[String]] = []
        
        var weightRange: [String] = []
        for temp in 35...100 {
            weightRange.append(String(temp))
        }
        weightData.append(weightRange)
        
        var numberList: [String] = []
        for num in 0...9 {
            numberList.append(String(num))
        }
        weightData.append(numberList)
        
        return weightData
    }
    
    func getTyres() -> [[String]] {
        var tyreData: [[String]] = []
        
        var tyreRange: [String] = []
        for temp in 1...100 {
            tyreRange.append(String(temp))
        }
        tyreData.append(tyreRange)
        
        var numberList: [String] = []
        for num in 0...9 {
            numberList.append(String(num))
        }
        tyreData.append(numberList)
        
        return tyreData
    }
    
    
}
